<?php

namespace App\Models\Template;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class TemplateDispatchnote extends Model
{
    protected $table = 'template_dispatchnote';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $status = 0, $isgetall = 0)
    {
        $cpid=0;
        $query = self::where(['cpid' => $cpid])->orderBy('create_at', 'desc');
        if (in_array($status, [1, 2])) {
            $query->where('status', $status);
        }
        $dispatchnote_no = $request->dispatchnote_no;
        if ($dispatchnote_no != '') {
            $query->where('dispatchnote_no', 'like', '%' . $dispatchnote_no . '%');
        }

        $warehouse_name = $request->warehouse_name;
        if ($warehouse_name != '') {
            $query->where('warehouse_name', 'like', '%' . $warehouse_name . '%');
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->select(['id', 'cpid', 'dispatchnote_no', 'warehouse_name', 'dispatchnote_filepath', 'status'])->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->select(['id', 'cpid', 'dispatchnote_no', 'warehouse_name', 'dispatchnote_filepath', 'status'])->get()->toArray();

            return ['list' => $list];
        }
    }
}
